package com.jielihaofeng.atguigu.nio.zerocopy;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * @author Johnnie Wind
 * @description new io 服务端
 * @date 2021/11/25 17:51
 */
public class NewIOServer {
    public static void main(String[] args) throws IOException {
        InetSocketAddress socketAddress = new InetSocketAddress(7001);
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        ServerSocket serverSocket = serverSocketChannel.socket();
        serverSocket.bind(socketAddress);

        ByteBuffer byteBuffer = ByteBuffer.allocate(4096);
        while (true){
            SocketChannel socketChannel = serverSocketChannel.accept();

            int readCount= 0;
            while (readCount!=-1){
                try {
                    readCount = socketChannel.read(byteBuffer);
                }catch (Exception ex){
                    ex.printStackTrace();
                    break;
                }
            }
            // 倒带
            byteBuffer.rewind();
        }
    }
}
